#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

char b[10][10];
char actor;
int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};
int dy[] = {0, 0, 1, -1, 1, -1, 1, -1};
bool check(char c, int x, int y) {
  if (b[x][y] != '-') return false;
  char oth = 'B' + 'W' - c;
  rep(i, 0, 7) {
    int j = 1;
    while (b[x + j * dx[i]][y + j * dy[i]] == oth) j++;
    if (j == 1) continue;
    if (b[x + j * dx[i]][y + j * dy[i]] != c) continue;
    return true;
  }
  return false;
}
void mov(char c, int x, int y) {
  b[x][y] = c;
  char oth = 'B' + 'W' - c;
  rep(i, 0, 7) {
    int j = 1;
    while (b[x + j * dx[i]][y + j * dy[i]] == oth) j++;
    if (j == 1) continue;
    if (b[x + j * dx[i]][y + j * dy[i]] != c) continue;

    j = 1;
    while (b[x + j * dx[i]][y + j * dy[i]] == oth) {
      b[x + j * dx[i]][y + j * dy[i]] = c;
      j++;
    }
  }
}
void lst() {
  vector<pii> res;
  rep(i, 1, 8) rep(j, 1, 8) {
    if (check(actor, i, j)) res.push_back(pii(i, j));
  }
  if (res.empty())
    cout << "No legal move." << endl;
  else {
    int n = res.size();
    rep(i, 0, n - 1) {
      cout << '(' << res[i].first << ',' << res[i].second << ')'
           << " \n"[i == n - 1];
    }
  }
}
void mov() {
  char c;
  int x, y;
  cin >> c;
  x = c - '0';
  cin >> c;
  y = c - '0';
  if (!check(actor, x, y)) actor = 'W' + 'B' - actor;
  mov(actor, x, y);
  actor = 'W' + 'B' - actor;
  int cntb = 0, cntw = 0;
  rep(i, 1, 8) rep(j, 1, 8) {
    if (b[i][j] == 'B') cntb++;
    if (b[i][j] == 'W') cntw++;
  }
  cout << "Black - " << setw(2) << cntb << ' ';
  cout << "White - " << setw(2) << cntw << endl;
}
void show() {
  rep(i, 1, 8) {
    rep(j, 1, 8) cout << b[i][j];
    cout << endl;
  }
}
void solve() {
  rep(i, 1, 8) rep(j, 1, 8) cin >> b[i][j];
  cin >> actor;
  char op;
  while (true) {
    cin >> op;
    if (op == 'Q') break;
    if (op == 'L') lst();
    if (op == 'M') mov();
  }
  show();
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  // freopen("1.txt", "r", stdin);
  // freopen("2.txt", "w", stdout);
  int t;
  cin >> t;
  rep(i, 1, t) {
    if (i > 1) cout << endl;
    solve();
  }
  return 0;
}